#!/bin/bash

if dpkg -l box64 &>/dev/null ;then
  sudo apt purge -y --allow-change-held-packages box64*
fi

add_external_repo "box64" "https://pi-apps-coders.github.io/box64-debs/KEY.gpg" "https://Pi-Apps-Coders.github.io/box64-debs/debian" "./" || exit 1

apt_update
if [ $? != 0 ]; then
  rm_external_repo "box64"
  error "Failed to perform apt update after adding box64 repository."
fi

# obtain SOC_ID
get_model
if [[ "$SOC_ID" == "tegra-x1" ]] || [[ "$SOC_ID" == "tegra-x2" ]]; then
  install_packages box64-tegrax1 || exit 1
elif [[ "$SOC_ID" == "rk3399" ]]; then
  install_packages box64-rk3399 || exit 1
elif [[ "$SOC_ID" == "bcm2711" ]]; then
  install_packages box64-rpi4arm64 || exit 1
elif [[ "$SOC_ID" == "bcm2837" ]]; then
  install_packages box64-rpi3arm64 || exit 1
elif cat /proc/cpuinfo | grep -q aes; then
  warning "There is no box64 pre-build for your device $SOC_ID $model"
  warning "Installing the generic arm box64 build as a fallback (crypto extensions enabled)"
  install_packages box64-generic-arm || exit 1
else
  warning "There is no box64 pre-build for your device $SOC_ID $model"
  warning "Installing the RPI4 tuned box64 build as a fallback (no crypto extensions enabled)"
  install_packages box64-rpi4arm64 || exit 1
fi
